<template>
  <div class="log-event-main">
    <div class="log-event-heider">
      <el-row>
        <el-col :span="11">
          <el-input placeholder="原始消息检索" v-model="formline.key" size="mini" class="defaultInput">
            <template slot="prepend">{{ searchParams }}</template>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="11" style="padding-left:10px;">
          <el-button-group>
            <el-button type="primary" size="mini" icon="el-icon-time">最近5分钟</el-button>
            <el-button type="primary" size="mini" icon="el-icon-refresh">刷新设置</el-button>
            <el-button type="primary" size="mini" icon="el-icon-refresh-left">重置</el-button>
            <el-button type="primary" size="mini" icon="el-icon-tickets">保存</el-button>
            <el-button type="primary" size="mini" icon="el-icon-download">下载</el-button>
          </el-button-group>
        </el-col>
        <el-col :span="2" style="text-align:right;">
          <el-button type="primary" size="mini" plain icon="el-icon-setting">管理</el-button>
        </el-col>
      </el-row>
      <el-row style="line-height:35px;">
        <el-col :span="24" class="params-col">
          <div class="params-list" v-for="(item, index) in searchParamsList" :key="index">
            <span class="text">{{item.name}}</span>
            <span class="op"> | <i class="el-icon-edit"></i> | <i class="el-icon-delete" @click="deleteParams(item)"></i> |</span>
          </div>
          <div class="params-select">
            <el-dropdown size="mini" split-button type="warning" @click="handleDropdownClick" @command="dropdownItemClick">
              <!-- <i class="el-icon-plus"></i> 字段过滤 -->
              <el-popover
                :value="showPopover"
                placement="bottom-start"
                width="650"
                trigger="click">
                <div class="logRuleTree" v-if="showSelectVisiable === 2">
                  <div class="title" style="margin-bottom:10px">
                    <i class="iconfont iconshaixuan"></i><span class="text">条件</span>
                  </div>
                  <log-rule-tree
                    :data="ruleData"
                    :dropData="dropData"
                    :filedData="filedData"
                  />
                  <div slot="footer" class="dialog-footer" style="text-align:right;margin-top:15px;">
                    <el-button
                      type="warning"
                      size="mini">确定</el-button>
                    <el-button
                      @click="showPopover = false"
                      size="mini">取消</el-button>
                  </div>
                </div>
                <log-params-input v-if="showSelectVisiable === 1" ref="paramsInputRef" :secParams="filedData" :dialogFormVisible="dialogFormVisible" @saveParamsChange="saveParamsFilter" @cancelParams="cancelParams"/>
                <!-- <log-params-input v-if="showSelectVisiable === 2"/> -->
                <span slot="reference"><i class="el-icon-plus" ></i> 字段过滤</span>
              </el-popover>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="1">字段过滤</el-dropdown-item>
                <el-dropdown-item command="2">高级过滤</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="log-event-table">
      <el-table
        border
        stripe
        :data="tableData"
        class="expandTable"
        style="width: 100%">
        <el-table-column type="expand">
          <template>
            <el-button-group>
              <el-button type="primary" size="mini">显示全部</el-button>
              <el-button type="primary" size="mini">日志泛化</el-button>
            </el-button-group>
            <div class="expand-main">
              <div class="expand-list" v-for="(item, index) in expandData" :key="index">
                <div class="expand-list-title">
                  {{item.title}}
                </div>
                <div class="expand-list-oprate">
                  <el-button type="text" @click.native="handleRowClick(item, 1)"><i class="el-icon-zoom-in"></i></el-button>
                  <el-button type="text" @click.native="handleRowClick(item, 2)"><i class="el-icon-zoom-out"></i></el-button>
                </div>
                <div class="expand-list-content">
                  {{item.content}}
                </div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="事件接收时间"
          min-width="16%"
          prop="id">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.date }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="设备IP"
          min-width="15%"
          prop="ip">
        </el-table-column>
        <el-table-column
          label="设备类型"
          min-width="10%"
          align="center"
          prop="type">
        </el-table-column>
        <el-table-column
          label="原始日志"
          min-width="55%"
          prop="log">
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.current"
        :page-sizes="[10, 25, 30, 40]"
        :page-size="page.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
// import util from '@/utils/util'
import LogParamsInput from '@/components/log-params-input'
import LogRuleTree from '@/components/log-rule-tree'
import {
  selectLogicData,
  getFieldData
} from '@/api/analysis/intelligent'
export default {
  props: {
    searchParams: {
      type: String,
      default: '默认'
    }
  },
  data() {
    return {
      showPopover: false,
      dialogFormVisible: true,
      ruleData: [
        {
          id: 1,
          label: '&& AND',
          type: 'AND',
          children: [],
          operands: []
        }
      ],
      dropData: [
        {
          value: 1,
          label: '逻辑运算',
          icon: 'el-icon-link',
          children: [
            {
              value: 'AND',
              label: '&& AND',
              type: 'AND'
            },
            {
              value: 'OR',
              label: '|| OR',
              type: 'OR'
            },
            {
              value: 'NOT',
              label: '!= NOT',
              type: 'NOT'
            }
          ]
        },
        {
          value: 2,
          label: '字段条件',
          icon: 'el-icon-discount'
        }
      ],
      filedData: [],
      showSelectVisiable: 1,
      formline: {
        key: ''
      },
      tableData: [
        { id: 1, date: '2020-05-06 10:14:45', ip: '192.168.34.17', type: 'vCenter', log: '<28>1 2020-05-06T09:20:31.009292+08:00 vcenter01 vmdird - - - t@140551781340928: No SD found for cn=DSE Root' },
        { id: 2, date: '2020-05-06 10:14:45', ip: '192.168.34.17', type: 'vCenter', log: '<28>1 2020-05-06T09:20:31.009292+08:00 vcenter01 vmdird - - - t@140551781340928: No SD found for cn=DSE Root' },
        { id: 3, date: '2020-05-06 10:14:45', ip: '192.168.34.17', type: '交换机', log: '<28>1 2020-05-06T09:20:31.009292+08:00 vcenter01 vmdird - - - t@140551781340928: No SD found for cn=DSE Root' },
        { id: 4, date: '2020-05-06 10:14:45', ip: '192.168.34.17', type: 'vCenter', log: '<28>1 2020-05-06T09:20:31.009292+08:00 vcenter01 vmdird - - - t@140551781340928: No SD found for cn=DSE Root' }
      ],
      expandData: [
        { id: 1, title: '事件ID', content: '1234567' },
        { id: 2, title: '事件接收时间', content: '2020-05-06 09:21:51' },
        { id: 3, title: '产生时间', content: '2020-05-06 09:21:51' },
        { id: 4, title: '事件名称', content: '重要事件' },
        { id: 5, title: '事件描述', content: 't@140551781340928: No SD found for cn=DSE Root' }
      ],
      loading: true,
      page: {
        current: 1,
        size: 10,
        total: 0
      },
      searchParamsList: []
    }
  },
  components: {
    LogParamsInput,
    LogRuleTree
  },
  created() {
    // 获取逻辑数据
    this.getLogicData()
    // 获取字段数据
    this.getFieldData()
  },
  methods: {
    // 获取逻辑数据
    getLogicData() {
      selectLogicData().then(res => {
        if (res) {
          res.forEach(element => {
            element.label = element.name + ' ' + element.value
          })
          this.dropData[0].children = res
        }
      })
    },
    // 获取字段条件中间下拉框的数据
    getFieldData() {
      getFieldData().then(res => {
        this.filedData = res
      })
    },
    // 分页变化改动
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`)
    },
    // table扩展数据点击
    handleRowClick(data, type) {
      let str = data.title + ' ' + (type === 1 ? '=' : '!=') + ' ' + data.content
      let params = {
        name: str,
        type: 'field',
        data: {
          left: data.title,
          name: type === 1 ? '=' : '!=',
          right: data.content
        }
      }
      this.concatParams(params)
    },
    // 删除字段条件
    deleteParams(data) {
      this.searchParamsList = this.searchParamsList.filter(item => {
        return data.name !== item.name
      })
    },
    // 下拉菜单左侧按钮点击
    handleDropdownClick() {
      this.showSelectVisiable = 1
      this.showPopover = true
    },
    // 下拉菜单选项点击
    dropdownItemClick(item) {
      this.showSelectVisiable = parseInt(item)
      this.showPopover = true
    },
    // 字段过滤时，数据保存
    saveParamsFilter(data) {
      console.log(data)
      let params = {
        name: data.label,
        type: 'field',
        data: data.formInline
      }
      this.concatParams(params)
    },
    // 查询条件数据拼接
    concatParams(data) {
      this.searchParamsList.push(data)
    },
    // 取消字段过滤
    cancelParams(flag, isEdit) {
      console.log(flag)
      // 判断是否是编辑时的取消
      if (isEdit) {
        this.$nextTick(() => {
          this.$set(this.editData.node, 'isEdit', false)
        })
      } else {
        this.showPopover = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.defaultInput .el-input-group__prepend{
  background: #919191 !important;
  color: #fff !important;
}
.log-event-table{
  margin-top:5px;
}
.expand-main {
  margin-top: 5px;
}
.expand-main .expand-list{
  display: flex;
  width: 100%;
  height: 35px;
  color: #fff;
  margin: 5px 0;
  border-radius: 5px;
  background: #b2b2b2;
  justify-content: flex-start;
  align-items: center;
  cursor: pointer;
  z-index: 999;
  &:hover{
    background: #e5e5e5;
  }
  .expand-list-title{
    width: 12%;
    margin-left: 20px;
  }
  .expand-list-oprate{
    width: 6%;
    z-index: 9999;
  }
  .expand-list-content{
    width: 75%;
  }
}
.expand-main .expand-list:nth-child(odd){
  background: #cccccc;
  &:hover{
    background: #e5e5e5;
  }
}
.params-col {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  .params-list {
    display: flex;
    height: 27px;
    line-height: 27px;
    font-size: 12px;
    color: #fff;
    padding: 0 5px;
    margin: 0 5px;
    background: #323232;
    border-radius: 5px;
    cursor: pointer;
    justify-content: flex-start;
    align-items: center;
    transition: width 2s ease;
    span.text{
      margin-right: 10px;
    }
    span.op {
      display: none;
      i{
        padding: 0 5px;
      }
    }
  }
  .params-list:hover{
    & span.op {
      display: block;
    }
  }
}
</style>
